<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user.ts'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'

let useStore = useUserStore()
// 定义变量控制加载的效果
let loading = ref(false)
// 获取路由器
let $router = useRouter()
let loginForms = ref()
let loginForm = reactive({
  username: 'admin',
  password: '123456',
})

// 自定义校验规则
const validatorUserName = (rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少是5位'))
  }
}

const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少是6位'))
  }
}
// 定义表单校验对象
const rules = reactive({
  username: [
    // {required: true, min: 6, max: 10, message: '账号长度至少6位',trigger:'change'}
    { validator: validatorUserName, trigger: 'change' },
  ],
  password: [
    // {required: true, min: 6, max: 15, message: "密码长度至少6位",trigger: 'change'}
    { validator: validatorPassword, trigger: 'change' },
  ],
})
// 登陆按钮回调
const login = async () => {
  await loginForms.value.validate(async (valid: boolean) => {
    if (valid) {
      // 开始加载
      loading.value = true
      try {
        await useStore.userLogin(loginForm)
        // 编程式导航跳转到首页
        await $router.push('/')
        ElNotification({
          type: 'success',
          message: '欢迎回来',
          title: `Hi ${getTime()}好`,
        })
      } catch (error) {
        ElNotification({
          type: 'error',
          message: (error as Error).message, // 类型断言一下类型
        })
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              show-password
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login_btn"
              type="primary"
              :loading="loading"
              @click="login"
            >
              登陆
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
